﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Domain;

namespace SharedGenomics.Annotations
{
    [Serializable]
    public class UcscBrowserAnnotation : IAnnotation, IWebSearchable
    {
        private const int BLOOM_DISTANCE = 5000;
        private int _chromosome;
        private long _chromosomePosition;
        private string _snpName;

        public UcscBrowserAnnotation(int chromosome, long chromosomePosition, string snpName)
        {
            this.ChromosomeIndex = chromosome;
            this.ChromosomePosition = chromosomePosition;
            this.SnpName = snpName;
        }

        #region IWebSearchable Members

        public Uri SearchUri
        {
            get
            {
                string uri = String.Format( "http://genome.ucsc.edu/cgi-bin/hgTracks?position=chr{0}:{1}-{2}&snp128=pack&hgFind.matches=rs{3}",
                                            this.ChromosomeIndex,
                                            Math.Max(0, this.ChromosomePosition - BLOOM_DISTANCE),
                                            this.ChromosomePosition + BLOOM_DISTANCE,
                                            this.SnpName ?? String.Empty);

                return new Uri(uri);
            }
        }

        public string SummaryText
        {
            get { return String.Format("Open Ucsc browser for {0}", this.SnpName ?? "<null>"); }
        }

        #endregion

        private int ChromosomeIndex
        {
            get { return this._chromosome; }
            set
            {
                if (Chromosome.IsValid(value))
                {
                    this._chromosome = value;
                }
                else
                {
                    throw new ArgumentOutOfRangeException(String.Format("{0} is not a valid chromosome index", value));
                }
            }
        }

        private long ChromosomePosition
        {
            get { return this._chromosomePosition; }
            set
            {
                if (value < 0)
                    throw new ArgumentOutOfRangeException("Chromosome position must be non-negative");

                this._chromosomePosition = value;
            }
        }

        private string SnpName
        {
            get { return this._snpName; }
            set { this._snpName = value; }
        }

        public AnnotationKey AnnotationKey
        {
            get { return AnnotationKey.UcscBrowser; }
        }

        public string Description
        {
            get { return String.Format("UCSC genome browser for {0}", this.SnpName ?? "<null>"); }
        }

        public string EncodedKey
        {
            get
            {
                return "<annotation type=\"Ucsc\"><snp>" + System.Security.SecurityElement.Escape(SnpName) + "</snp><url>" + System.Security.SecurityElement.Escape(SearchUri.ToString()) + "</url></annotation>";
            }
        }
    }
}
